package com.tusiyou.homework;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class HomeFragment extends Fragment {

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private ViewPager viewPager;
    private List<View> mLists;
    private RadioGroup rg;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.fragment_home, container, false);

        return ll;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        rg = (RadioGroup) view.findViewById(R.id.rg_home);
        viewPager = (ViewPager) view.findViewById(R.id.home_viewpager);
        RadioButton rb = (RadioButton) rg.getChildAt(0);
        rb.setChecked(true);

        initViewPagerData();

        MyAdapter adapter = new MyAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton rb = (RadioButton) rg.getChildAt(position);
                rb.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rb = (RadioButton) group.findViewById(checkedId);
                int index = group.indexOfChild(rb);
                viewPager.setCurrentItem(index);

            }
        });


    }

    private void initViewPagerData() {
        mLists = new ArrayList<View>();



        List<HashMap<String,Object>> mapList = new ArrayList<>();
        for (int i = 0;i<100;i++){
            HashMap<String,Object> map = new HashMap<>();
            map.put("image",R.mipmap.ic_launcher);
            mapList.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(getActivity(),mapList,R.layout.home_newest_item,new String[]{"image"},new int[]{R.id.home_newest_image});


        for(int i=0; i<rg.getChildCount();i++){
            View v = getActivity().getLayoutInflater().inflate(R.layout.home_newest,null);
            GridView gridView = (GridView) v.findViewById(R.id.home_gv);
            gridView.setAdapter(adapter);
            mLists.add(v);
        }

    }

    public class MyAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return mLists.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mLists.get(position);
            container.addView(view);
            return view;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }


}
